<template>
	<view>
		<u-cell-group>
			<u-cell title="头像" isLink @click="updateImage">
				<image mode="aspectFill" class="tou-img" slot="value" :src="srcImg"></image>
			</u-cell>
			<u-cell @click="updateMessage('修改姓名','nickname')" size="large" title="姓名" :value="form.nickname" isLink>

			</u-cell>
			<u-cell size="large" title="账号" :value="form.username" @longpress="insideCode">

			</u-cell>
			<u-cell @click="showPassword = true" size="large" title="密码" :value="form.password" isLink>

			</u-cell>
			<u-cell size="large" title="账号状态" :value="form.status">

			</u-cell>
			<u-cell size="large" title="项目" :value="form.leader">

			</u-cell>
			<u-cell size="large" title="年龄" :value="form.age">

			</u-cell>
			<u-cell size="large" title="性别" :value="form.sex">

			</u-cell>
			<u-cell size="large" title="手机号" :value="form.phone">

			</u-cell>
		</u-cell-group>
		<view class="btn-go-out">
			<view class="btn"><button type="primary" @click="goOut"> 退出登录 </button></view>
		</view>
		<view>
			<u-modal :show="show" :title="title" showCancelButton='true' @cancel="offShow" @confirm="okNickName">
				<u--input placeholder="请输入内容" border="surround" v-model="form.nickname"></u--input>
			</u-modal>
		</view>
		<view>
			<u-modal :show="showPassword" title="修改密码" showCancelButton='true' @cancel="showPassword = false"
				@confirm="okPssword">
				<view class="w750 h300">
					<view class="w100">
						<u-input password='true' placeholder="请输入原始密码" border="surround" v-model="password"></u-input>
					</view>
					<view class="w100 mg-top-20">
						<u-input password='true' placeholder="请输入新密码" border="surround" v-model="password1"></u-input>
					</view>
					<view class="w100 mg-top-20">
						<u-input password='true' placeholder="请再次输入新密码" border="surround" v-model="password2"></u-input>
					</view>
				</view>
			</u-modal>
		</view>
		<view>
			<u-modal :show="insideShow" title="蓝牙名称" showCancelButton='true' @cancel="insideShow = false"
				@confirm="okinside">
				<view class="w750 h300">
					<view class="w100">
						<u-input password='true' placeholder="请输入名称" border="surround" v-model="lanyaname"></u-input>
					</view>
				</view>
			</u-modal>
		</view>
		<view>
			<u-modal :show="testShow" title="操作" showCancelButton='true' @cancel="testShow = false"
				@confirm="testShow = false">
				<view class="w750 h300">
					<view class="w100">
						<button type="primary" @click="loginot"> 蓝牙DL </button>
						<button type="primary" @click="loginShan"> 蓝牙SH </button>
						<button type="primary" @click="loginXi"> 蓝牙XB </button>
					</view>
				</view>
			</u-modal>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				testShow: false,
				insideShow: false,
				lanyaname: '',
				srcImg: '../../static/images/my-on.png',
				show: false,
				showPassword: false,
				title: '信息修改',
				form: {
					username: uni.getStorageSync('username'),
					password: '********',
					status: '暂无信息',
					leader: '暂无信息',
					age: '暂无信息',
					sex: '暂无信息',
					phone: '暂无信息',
					nickname: '暂无信息'
				},
				password: '',
				password1: '',
				password2: ''
			}
		},
		mounted() {
			this.getMessage()
		},
		onShareAppMessage: function() {

		},
		methods: {
			// 获取个人详情
			getMessage() {
				let userData = uni.getStorageSync('userData')
				this.getTouImage(userData.profilePicture)
				this.form.leader = userData.leader
				this.form.age = userData.age
				this.form.phone = userData.phone
				this.form.status = userData.status == "0" ? "正常" : "禁用"
				this.form.sex = userData.sex == '0' ? "男" : "女"
				this.form.nickname = userData.nickname
			},
			// 关闭修改姓名窗口
			offShow() {
				this.show = false
			},
			// 点击确定按钮
			okNickName() {
				let userData = uni.getStorageSync('userData')
				userData.nickname = this.form.nickname
				this.updateOtherMessage(userData)
			},
			// 修改个人信息---姓名
			updateMessage(tit, attribute) {
				this.title = tit
				this.show = true
			},
			// 修改密码
			okPssword() {
				if (this.password1 === this.password2) {
					if (this.password === this.password1) {
						uni.$u.toast("新密码不能与旧密码相同")
					} else {
						let userData = uni.getStorageSync('userData')
						userData.password = this.password1
						this.$post('/security/users/updatePw?password=' + this.password,
							userData
						).then(res => {
							if (res.code == 200) {
								uni.$u.toast("密码修改成功！！！")
								this.showPassword = false
							} else {
								uni.$u.toast(res.message)
							}
						})
					}
				} else {
					uni.$u.toast("两次输入的新密码不一致请重新填写")
				}
			},
			updateImage() {
				let _this = this
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: function(res) {
						_this.updateImg(res)
					}
				})
			},
			// 上传头像
			updateImg(val) {
				let _this = this
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: 'https://www.xiaowu-iot.com/api/admin/file/upload/picture',
						filePath: val.tempFilePaths[0],
						name: 'file',
						formData: {
							file: val.tempFiles[0]
						},
						success: (resd) => {
							let data = JSON.parse(resd.data)
							this.upadteTouImageMessage(data.data.name)
							resolve(data.data)
							console.log(`上传成功提示信息${resd.message}`)
						},
						error: (re) => {
							console.log(re)
						}
					})
				})
			},
			// 修改头像信息
			upadteTouImageMessage(name) {
				let userData = uni.getStorageSync('userData')
				userData.profilePicture = name
				this.$put('/security/users/' + userData.userId, userData).then(res => {
					if (res.code == 200) {
						uni.setStorageSync('userData', res.data)
						this.getTouImage(name)
					}
				})

			},
			// 获取头像
			getTouImage(val) {
				this.$get('/admin/file/getOutsideChain', {
					fileName: val,
				}).then(res => {
					this.srcImg = res.data.outsideChain
				})
			},
			// 修改其他信息
			updateOtherMessage(val) {
				let userData = uni.getStorageSync('userData')
				this.$put('/security/users/' + userData.userId, val).then(res => {
					if (res.code == 200) {
						uni.setStorageSync('userData', res.data)
						this.show = false
						uni.$u.toast("修改成功")
					}
				})
			},
			// 退出登录
			goOut() {
				// 清除存储数据
				uni.clearStorageSync()
				uni.navigateTo({
					url: '../login/login'
				})
			},
			insideCode() {
				this.insideShow = true
			},
			okinside() {
				if (this.lanyaname == '205002') {
					this.insideShow = false
					this.testShow = true
				} else {
					uni.$u.toast("错误提醒！")
				}
			},
			loginot() {
				this.$post1('/api/user/doLogin', {
					"account": "zhaoyipeng",
					"password": "1234"
				}).then(res => {
					if (res.success) {
						uni.setStorageSync('tokenTest', res.data)
						uni.$u.toast("成功！！")
					} else {
						uni.$u.toast("失败！！")
					}
				})
			},
			loginShan() {
				this.$post1('/api/attendance/punchIn', {}).then(res => {
					if (res.success) {
						uni.$u.toast("成功！！")
					} else {
						uni.$u.toast(res.message)
					}
				})
			},
			loginXi() {
				this.$post1('/api/attendance/punchOut', {}).then(res => {
					if (res.success) {
						uni.$u.toast("成功！！")
					} else {
						uni.$u.toast(res.message)
					}
				})
			}
		}

	}
</script>

<style scoped>
	.tou-img {
		width: 120rpx;
		height: 120rpx;
	}

	.btn-go-out {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 750rpx;
		margin-top: 80rpx;
	}

	.btn {
		width: 600rpx;
	}
</style>